
using OhioState.AssetManaged;
using OhioState.Graphics;
using OhioState.Graphics.Proxy;
using System.Collections.Generic;
using OhioState.Math;
using OhioState.Math.ThreeDimensional;


namespace OhioState.Graphics.OpenGL
{
    public class AxesPlaneHandler : OpenGLObject, IDrawable
    {
        private AxesPlaneProxy _axesPlaneProxy;
        private AxesPlaneGL _realAxesPlane;
        private QuadList axesPlaneGeometry = new QuadList(48);
        public AxesPlaneHandler( AxesPlaneProxy proxyObj )
            : base(proxyObj)
        {
            _axesPlaneProxy = proxyObj;
            _axesPlaneProxy.FindIntersections();
            _realAxesPlane = new AxesPlaneGL(_axesPlaneProxy.XYSlice, _axesPlaneProxy.XZSlice, _axesPlaneProxy.YZSlice);
        }
        public void Render(IRenderPanel panel)
        {
            Update(panel);
            //_realAxesPlane.Render(panel);
            axesPlaneGeometry.Render(panel);
                
        }

        public override void Update(IRenderPanel panel)
        {
            if (this.Invalid)
            {
                this.Invalid = false;
                FindIntersections(_axesPlaneProxy);
                _realAxesPlane.Material = panel.ResourceManager.GetItem(_axesPlaneProxy.Material as IAssetManaged) as IMaterial;
                axesPlaneGeometry.Material = panel.ResourceManager.GetItem(_axesPlaneProxy.Material as IAssetManaged) as IMaterial;
            }
        }

        private static Direction xAxis = new Direction(1, 0, 0);
        private static Direction yAxis = new Direction(0, 1, 0);
        private static Direction zAxis = new Direction(0, 0, 1);

        public void FindIntersections(AxesPlaneProxy slp)
        {
            axesPlaneGeometry.Clear();
            slp.FindIntersections();
            IList<Vector3> xySlice = slp.XYSlice;
            IList<Vector3> xzSlice = slp.XZSlice;
            IList<Vector3> yzSlice = slp.YZSlice;
            foreach (Vector3 point in xySlice)
            {
                Vertex vertex = new Vertex();
                vertex.Position = new Point(point);
                vertex.Normal = zAxis;
                axesPlaneGeometry.AddVertex(vertex);
            }
            foreach (Vector3 point in xzSlice)
            {
                Vertex vertex = new Vertex();
                vertex.Position = new Point(point);
                vertex.Normal = yAxis;
                axesPlaneGeometry.AddVertex(vertex);
            }
            foreach (Vector3 point in yzSlice)
            {
                Vertex vertex = new Vertex();
                vertex.Position = new Point(point);
                vertex.Normal = xAxis;
                axesPlaneGeometry.AddVertex(vertex);
            }
        }

        public IMaterial Material
        {
            get { return _axesPlaneProxy.Material; }
            set { }
        }
        public IBoundingVolume BoundingVolume
        {
            get { return _axesPlaneProxy.BoundingVolume; }
        }

        public int DrawablePosition
        {
            get { return _realAxesPlane.DrawablePosition; }
            set { _realAxesPlane.DrawablePosition = value; }
        }
       
    }
}
